import { Component } from '@angular/core';
import { NavController, NavParams, Platform } from 'ionic-angular';
import { JPushService } from 'ionic2-jpush';

@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})

export class ListPage {
  selectedItem: any;
  icons: string[];
  items: Array<{ title: string, note: string, icon: string }>;

  constructor(public navCtrl: NavController, public navParams: NavParams,
    private platform: Platform,
    private jPushPlugin: JPushService) {
    // If we navigated to this page, we will have an item available as a nav param
    this.selectedItem = navParams.get('item');

    // Let's populate this page with some filler content for funzies
    this.icons = ['flask', 'wifi', 'beer', 'football', 'basketball', 'paper-plane',
      'american-football', 'boat', 'bluetooth', 'build'];

    this.items = [];
    for (let i = 1; i < 11; i++) {
      this.items.push({
        title: 'Item ' + i,
        note: 'This is item #' + i,
        icon: this.icons[Math.floor(Math.random() * this.icons.length)]
      });
    }
    platform.ready().then(() => {
        // jpush.setTagsWithAlias：设置别名和标签时触发
        // jpush.openNotification：打开推送时触发
        // jpush.receiveNotification：接收到通知时触发
        // jpush.receiveMessage：接收到消息时触发
      this.jPushPlugin.openNotification()
        .subscribe(res => {
          console.log('收到推送');
          console.log(res)
        });

      this.jPushPlugin.receiveNotification()
        .subscribe(res => {
          console.log('收到推送');
          console.log(res)
        });

      this.jPushPlugin.receiveMessage()
        .subscribe(res => {
          console.log('收到推送');
          console.log(res)
        });

    })

  }

  itemTapped(event, item) {
    // That's right, we're pushing to ourselves!
    this.navCtrl.push(ListPage, {
      item: item
    });
  }

  /**
     * 注册极光
     */
  init() {
    this.jPushPlugin.init()
      .then(res => alert(res))
      .catch(err => alert(err))
  }

  /**
  * 获取ID
  */
  getRegistrationID() {
    this.jPushPlugin.getRegistrationID()
      .then(res => alert(res))
      .catch(err => alert(err))
  }

  /**
  * 设置 setBadge
  * 仅限IOS
  * @param number
  */
  setBadge(number) {
    if (this.platform.is('ios')) {
      this.jPushPlugin.setApplicationIconBadgeNumber(number)
        .then(res => alert(res))
        .catch(err => alert(err))
    }
  }
}
